package cn.luoshaocheng.org.controller;

import cn.luoshaocheng.basic.annotation.PreAuthorize;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.org.domain.Department;
import cn.luoshaocheng.org.query.DepartmentQuery;
import cn.luoshaocheng.org.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department loadById(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除",sn = "department:delete") //value后面用来做权限校验
    public jsonResult deleteById(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param department
     * @return
     */
    @PutMapping
    @PreAuthorize(name = "部门添加和修改",sn = "department:addORupdate") //value后面用来做权限校验
    public jsonResult addAndUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param departmentQuery
     * @return
     */
    @PostMapping

    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 查询部门树
     *
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "部门树",sn = "department:deptTree") //value后面用来做权限校验
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}

